<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
class WxbbController extends WeixinController {

	//线上门市提醒
	public function fenpei($id){
		$appid       = $this->appid;
		$template_id = 'WGLY6bVgJKufMu6duJHWTbUYejzS9AMsFJ47T211rDM'; //模板ID
		$url         = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=http://yw.gv-photo.com/index.php?s=/Home/Yewu/index/id/'.$id.'&response_type=code&scope=snsapi_base&state='.$data['jb'].'#wechat_redire';
		//发送微信接口
		$openid = 'xsjd';
		$data   = $this->shuju($id,$openid);
		$res    = $this->mb($data['openid'],$template_id,$url,$data['xsjd'],$data['ddbh'],$data['name'],$data['phone'],$data['qq'],$data['qd'],$data['lb'],$data['kf'],$data['bz'],$data['cztime']);
		return $res;
	}
	//成交提醒（提醒人 线上门市、线上经理、线下经理、店长、老板）
	public function cjtx($id){
		$template_id = 'Xe8Nh94pzzrwgW-3PYY9w_2ThZGluWgZ1-ksMJzkxgA'; //模板ID
		//发送微信接口
		$openid = 'xsjd';
		$data   = $this->shuju($id,$openid);
		$kefu   = D('Member')->field('openid,jb_id,uid')->select();
		$wxid   = array();
		$res 	= array();
		foreach ($kefu as $key=>$value){
			//线上门市
			if($value['openid'] == $data['openid']){
				$wxid[]  = $data['openid'];
			}
			//线下门市
			if($value['uid'] == $data['xxjd']){
				$wxid[]  = $data['openid'];
			}
			//线上经理
			if($value['jb_id'] == 3){
				$wxid[]  = $value['openid'];
			}
			//线下经理
			if($value['jb_id'] == 6){
				$wxid[]  = $value['openid'];
			}
			//老板
			if($value['jb_id'] == 7){
				$wxid[]  = $value['openid'];
			}
			//店长
			if($value['jb_id'] == 9){
				$wxid[]  = $value['openid'];
			}
		}
		foreach ($wxid as $key=>$value){
			$res[] = $this->chenjiaotx($value,$template_id,$url,$data['djcj'],$data['ddbh'],$data['ddtime'],$data['xsjd'],$data['xxjd'],$data['lb'],$data['money'],$data['zfkx'],$data['zffs'],$data['ywzt'],$data['pdtime'],$data['qd'],$data['xxbz']);
		}
		//$res = $weixin->cjtx($data['openid'],$template_id,$url,$data['djcj'],$data['ddbh'],$data['ddtime'],$data['xsjd'],$data['xxjd'],$data['lb'],$data['money'],$data['zfkx'],$data['zffs'],$data['ywzt'],$data['pdtime'],$data['qd'],$data['bz']);
		return $res;
	}
	//流失提醒（提醒人 线上门市、线上经理、线下经理、店长、老板）
	public function lstx($id){
		$template_id = 'THMi7Ks583Gwiib-71UrWLjJZuH4vVAAjjHYuG3sqvQ'; //模板ID
		//发送微信接口
		$openid = 'xsjd';
		$data   = $this->shuju($id,$openid);
		$kefu   = D('Member')->field('openid,jb_id,uid')->select();
		$wxid   = array();
		$res 	= array();
		foreach ($kefu as $key=>$value){
			//线上门市
			if($value['openid'] == $data['openid']){
				$wxid[]  = $data['openid'];
			}
			//线下门市
			if($value['uid'] == $data['xxjd']){
				$wxid[]  = $data['openid'];
			}
			//线上经理
			if($value['jb_id'] == 3){
				$wxid[]  = $value['openid'];
			}
			//线下经理
			if($value['jb_id'] == 6){
				$wxid[]  = $value['openid'];
			}
			//老板
			if($value['jb_id'] == 7){
				$wxid[]  = $value['openid'];
			}
			//店长
			if($value['jb_id'] == 9){
				$wxid[]  = $value['openid'];
			}
		}
		foreach ($wxid as $key=>$value){
			$res[] = $this->liushitx($value,$template_id,$url,$data['djls'],$data['ddbh'],$data['lstime'],$data['kf'],$data['xsjd'],$data['xxjd'],$data['lb'],$data['ywzt'],$data['qd'],$data['bz'],$data['xxbz']);
		}
		return $res;
	}
	//死单提醒（提醒人 线上门市、线上经理、线下经理、店长、老板）
	public function sdtx($id){
		$template_id = 'tRcgSzjZ0hXVrHbL7IPfVuzjPtxdM2Zpb_C-LMhnQmg'; //模板ID
		//发送微信接口
		$openid = 'xsjd';
		$data   = $this->shuju($id,$openid);
		$kefu   = D('Member')->field('openid,jb_id,uid')->select();
		$wxid   = array();
		$res 	= array();
		foreach ($kefu as $key=>$value){
			//线上门市
			if($value['openid'] == $data['openid']){
				$wxid[]  = $data['openid'];
			}
			//线下门市
			if($value['uid'] == $data['xxjd']){
				$wxid[]  = $data['openid'];
			}
			//线上经理
			if($value['jb_id'] == 3){
				$wxid[]  = $value['openid'];
			}
			//线下经理
			if($value['jb_id'] == 6){
				$wxid[]  = $value['openid'];
			}
			//老板
			if($value['jb_id'] == 7){
				$wxid[]  = $value['openid'];
			}
			//店长
			if($value['jb_id'] == 9){
				$wxid[]  = $value['openid'];
			}
		}
		foreach ($wxid as $key=>$value){
			$res[] = $this->liushitx($value,$template_id,$url,$data['djsd'],$data['ddbh'],$data['lstime'],$data['kf'],$data['xsjd'],$data['xxjd'],$data['lb'],$data['ywzt'],$data['qd'],$data['bz'],$data['xxbz']);
		}
		return $res;
	}
	//查询数据
	public function shuju($id,$openid){
		$yewu[] = D('Yewu')->where(array('id'=>array('eq',$id)))->find();
		$kefu   = D('Member')->field('uid,nickname,openid,jb_id')->select();
		$status = D('Ywlb')->where(array('status'=>array('eq',1)))->field('uid,name')->select(); 
		// 当天的零点
		$dayBegin = strtotime(date('Y-m-d', time()));

		// 当天的24
		$dayEnd = $dayBegin + 24 * 60 * 60;
		foreach ($yewu as $key => $x) {
			foreach ($status as $key => $y) {
				if($x['ywstate'] == $y['uid']){
					$x['ywzt'] = $y['name'];
				}
				if($x['qd'] == $y['uid']){
					$x['qd']    = $y['name'];
				}
				if($x['lb'] == $y['uid']){
					$x['lb'] = $y['name'];
				}
				if($x['zfkx'] == $y['uid']){
					$x['zfkx'] = $y['name'];
				}
				if($x['zffs'] == $y['uid']){
					$x['zffs'] = $y['name'];
				}
				
			}
			foreach ($kefu as $key => $v) {
				if($x['xsjd'] == $v['uid']){
					$x['xsjd'] = $v['nickname'];
					if($openid == 'xsjd'){
						$x['openid'] = $v['openid'];
					}	
				}
				if($x['xxjd'] == $v['uid']){
					$x['xxjd'] = $v['nickname'];
					$x['xianxiajd'] = $v['uid'];
					if($openid == 'xxjd'){
						$x['openid'] = $v['openid'];
					}
					
				}
				if($x['kf'] == $v['uid']){
					$x['kf'] = $v['nickname'];
					if($openid == 'kf'){
						$x['openid'] = $v['openid'];
					}
				}				
			}
			$x['djcj']   = D('Yewu')->where(array('ddtime'=>array('between',array($dayBegin,$dayEnd))))->count();
			$x['djls']   = D('Yewu')->where(array('lstime'=>array('between',array($dayBegin,$dayEnd))))->count();
			$x['djsd']   = D('Yewu')->where(array('sdtime'=>array('between',array($dayBegin,$dayEnd))))->count();
			if($x['pdtimes'] > 1){
				$x['pdtime'] = date('Y-m-d',$x['pdtimes']);
			}else{
				$x['pdtime'] = "没有填写排单时间";
			}
			$x['ddtime'] = date('Y-m-d G:i:s',$x['ddtime']);
			$x['lstime'] = date('Y-m-d G:i:s',$x['lstime']);
			$x['sdtime'] = date('Y-m-d G:i:s',$x['sdtime']);
			$x['cztime'] = date('Y-m-d G:i:s',$x['cztime']);
			$x['yytime'] = date('Y-m-d',$x['yytime']);
			$data = $x;
		}
		return $data;
	}
	
	
}